﻿<%@ Application Language="C#" %>
<script RunAt="server">

    //protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    //{
    //    // Let's write a message to show this got fired---
    //    //Response.Write("SessionID: " + Session.SessionID.ToString() + "User key: " + (string)Session["UserId"]);
    //    if (Session["UserId"] != null) // e.g. this is after an initial logon
    //    {
    //        string sKey = (string)Session["UserId"];
    //        // Accessing the Cache Item extends the Sliding Expiration automatically
    //        string sUser = (string)HttpContext.Current.Cache[sKey];
    //    }
    //}
    string IsCXTHLive = ConfigurationManager.AppSettings["TH.IsCXLive"].ToString();
    void Application_BeginRequest(object sender, EventArgs e)
    {
        string _requestUrl = Request.Url.ToString().ToLower();
        if (IsCXTHLive == "1")
        {
            //Sakthi on 07 Aug 2014 - Start
            if (_requestUrl.Contains("http://gluaygluay.com"))
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Status = "301 Moved Permanently";
                HttpContext.Current.Response.AddHeader("Location", _requestUrl.Replace("http://gluaygluay.com", "http://www.gluaygluay.com"));
            }
            if (_requestUrl.Contains(".aspx/"))
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Status = "301 Moved Permanently";
                HttpContext.Current.Response.AddHeader("Location", _requestUrl.Replace(".aspx/", ".aspx"));
            }          
            //if (_requestUrl.IndexOf("http://203.142.18.111") < 0)
            //{
            //    if (_requestUrl.IndexOf("http://www.") < 0)
            //    {
            //        Response.Redirect(_requestUrl.Replace("http://", "http://www."));
            //    }                
            //}
            //else
            //{
            //    Response.Redirect(_requestUrl.Replace("http://203.142.18.111", "http://www.gluaygluay.com"));
            //}
            //Sakthi on 07 Aug 2014 - End
        }

        bool _IsWorldCup = false;
        bool _IsBCI = false;
        string WC = "worldcup";
        string[] _strWorldCup = _requestUrl.ToLower().Split('/');
        foreach (string element in _strWorldCup)
        {
            WC = element;
            if (WC == "worldcup" || WC == "savings" || WC == "best-car-insurance")
            {
                _IsWorldCup = true;
            }
            if (WC == "best-car-insurance")
            {
                _IsBCI = true;
            }
        }
        if (_IsBCI)
        {
            if (CE.Global.General.isMobileBrowser())
            {
                string mobileurl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MobileBestCarURL");
                Response.Redirect(mobileurl);
            }
        }
        if (!_IsWorldCup)
        {
            if (!Request.Url.ToString().Contains("car-insurance-kb"))
            {
                if (CE.Global.General.isMobileBrowser())
                {
                    string mobileurl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MobileURL");
                    Response.Redirect(mobileurl);
                }
            }
        }

        
        string _teststr = _requestUrl.ToLower().Substring(_requestUrl.Length - 5, 5);
        if (_teststr == ".aspx")
        {
            if (!CE.Global.General.CheckAllLowerCase(_requestUrl))
            {
                Response.Redirect(_requestUrl.ToLower());
            }
        }

        string fullOrigionalpath = Request.Url.ToString();
        if (fullOrigionalpath.EndsWith("/"))
        {
            //fullOrigionalpath = fullOrigionalpath.Substring(0, fullOrigionalpath.Length - 1);
        }
        else
        {
            string[] _arrPageList = fullOrigionalpath.Split(';');
            if (fullOrigionalpath.Contains("404") && _arrPageList.Length == 2)
            {
                Response.Redirect(_arrPageList[1].ToLower() + "/");
            }
        }

        fullOrigionalpath = fullOrigionalpath.ToLower();
        
        if (fullOrigionalpath.EndsWith("/en/"))
        {
            Context.RewritePath("/home.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/car-details/"))
        {
            Context.RewritePath("/car-insurance/car-details.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/about-us/"))
        {
            Context.RewritePath("/about-us/about-us.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/contact-us/"))
        {
            Context.RewritePath("/contact-us/contact-us.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/faq/"))
        {
            Context.RewritePath("/faq/faq.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/news/"))
        {
            Context.RewritePath("/news/news.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/privacy/"))
        {
            Context.RewritePath("/privacy/privacy.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/terms/"))
        {
            Context.RewritePath("/terms/terms.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/sitemap/"))
        {
            Context.RewritePath("/sitemap/sitemap.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/signup-promo/"))
        {
            Context.RewritePath("/signup-promo/promotion.aspx?lang=en-US");
        }
        else if (Request.RawUrl.ToLower().EndsWith("contact-us/contactus"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("contact-us/contactus", "contact-us/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("contact-us/contact-us"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("contact-us/contact-us", "contact-us/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("en/contactus"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("en/contactus", "en/contact-us"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("about-us/aboutus"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("about-us/aboutus", "about-us/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("about-us/about-us"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("about-us/about-us", "about-us/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("news/news"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("news/news", "news/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("terms/terms"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("terms/terms", "terms/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("privacy/privacy"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("privacy/privacy", "privacy/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("faq/faq"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("faq/faq", "faq/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("sitemap/sitemap"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("sitemap/sitemap", "sitemap/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("careers/careers"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("careers/careers", "careers/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("motor-fleet-insurance/motor-fleet-insurance"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("motor-fleet-insurance/motor-fleet-insurance", "motor-fleet-insurance/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("passwordrecovery"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("passwordrecovery", "password-recovery/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("/home"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("/home", "/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("/en"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("/en", "/en/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("/best-car-insurance"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("/best-car-insurance", "/best-car-insurance/"));
        }
        else if (Request.RawUrl.ToLower().EndsWith("partners/partners"))
        {
            Response.Redirect(Request.RawUrl.ToLower().Replace("partners/partners", "partners/"));
        }
        else
        {
            ThunderMain.URLRewriter.Rewriter.Process();
        }
    }

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e)
    {
        CE.Database.MyDatabase _myDb = new CE.Database.MyDatabase();

        // Code that runs when an unhandled error occurs
        Exception objErr = Server.GetLastError().GetBaseException();
        string err;
        err = "IP : " + Request.UserHostAddress;
        err = err + System.Environment.NewLine;
        err = err + "Error in: " + Request.Url.ToString() +
                          ". Error Message:" + objErr.Message.ToString();

        //// An error has occured on a .Net page.
        var serverError = Server.GetLastError() as HttpException;

        if (null != serverError)
        {
            int errorCode = serverError.GetHttpCode();
            // Log the error
            _myDb.CreateErrorLog(errorCode.ToString() + "_", err);
            if (404 == errorCode)
            {
                //Server.ClearError();
                //Server.Transfer("404-new.aspx");
                string _requestUrl = Request.Url.ToString().ToLower();
                string _teststr = _requestUrl.ToLower().Substring(_requestUrl.Length - 5, 5);
                if (_teststr == ".aspx")
                {
                    if (!_requestUrl.Contains("/.aspx"))
                    {
                        Server.ClearError();
                        _requestUrl = _requestUrl.Replace(".aspx", "/");
                        Response.Redirect(_requestUrl);
                    }
                    else
                    {
                        Server.ClearError();
                        _requestUrl = _requestUrl.Replace("/.aspx", ".aspx");
                        Response.Redirect(_requestUrl);
                    }
                }
                else
                {
                    Server.Transfer("404.aspx");
                }
            }
            else if (403 == errorCode)
            {
                Server.ClearError();
                Server.Transfer("403.aspx");
            }
        }
        else
        {
            // Log the error
            _myDb.CreateErrorLog("Global_", err);
        }

        _myDb = null;
    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

</script>
